package cn.sq.musicserver.plug.model.jsososo.handle;

import cn.sq.musicserver.exception.PlugError;
import cn.sq.musicserver.plug.model.jsososo.constant.JsososoConstant;
import cn.sq.musicserver.plug.model.jsososo.entity.*;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SQ
 * Date: 2020/12/17
 * Time: 13:27
 * Description:请求处理
 */
@Slf4j
@Component
public class JsososoService {
    /**
     * 搜索音乐
     *
     * @param searchParam 搜索参数
     * @return 搜索结果
     * @throws IOException
     */
    public SearchResult search(SearchParam searchParam) throws PlugError {
        Connection connect = Jsoup.connect(JsososoConstant.SEARCH);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);
        connect.userAgent(JsososoConstant.USERAGENT);
        connect.headers(JsososoConstant.HEADERS);
        if (StringUtils.isEmpty(searchParam.getType_p())) {
            throw new PlugError("请选择搜索的平台");
        }
        connect.data("_p", searchParam.getType_p());
        connect.data("_t", LocalDateTime.now().getSecond() + "");
        if (StringUtils.isEmpty(searchParam.getKey())) {
            throw new PlugError("请填写搜索内容");
        }
        connect.data("key", searchParam.getKey());
        if (StringUtils.isNotEmpty(searchParam.getPageNo())) {
            connect.data("pageNo", searchParam.getPageNo());
        }
        if (StringUtils.isNotEmpty(searchParam.getPageSize())) {
            connect.data("pageSize", searchParam.getPageSize());
        }
        if (StringUtils.isNotEmpty(searchParam.getType())) {
            connect.data("type", searchParam.getType());
        } else {
            connect.data("type", "0");
        }
        Document document = null;
        try {
            document = connect.timeout(100000).get();
        } catch (IOException e) {
            log.error("获取数据异常:"+e.getMessage());
          throw  new  PlugError("获取数据异常");
        }
        return new Gson().fromJson(document.body().text(), SearchResult.class);
    }

    /**
     * 下载音乐
     *
     * @param downloadParam 下载参数
     * @return 下载地址
     * @throws IOException
     */
    public DownloadResult download(JsososoDownloadParam downloadParam) throws PlugError {
        Connection connect = Jsoup.connect(JsososoConstant.DOWNLOAD);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);
        connect.userAgent(JsososoConstant.USERAGENT);
        connect.headers(JsososoConstant.HEADERS);
        if (StringUtils.isEmpty(downloadParam.getType_p()) || StringUtils.isEmpty(downloadParam.getBr()) || StringUtils.isEmpty(downloadParam.getId())) {
            throw new PlugError("请填写相关参数");
        }
        connect.data("id", downloadParam.getId());
        if (StringUtils.isNotEmpty(downloadParam.getMediaId())) {
            connect.data("mediaId", downloadParam.getMediaId());
        } else {
            connect.data("mediaId", "undefined");
        }
        connect.data("br", downloadParam.getBr());
        connect.data("_p", downloadParam.getType_p());
        connect.data("_t", LocalDateTime.now().getSecond() + "");
        try {
            return new Gson().fromJson(connect.get().body().text(), DownloadResult.class);
        } catch (IOException e) {
            log.error("插件运行异常："+e.getMessage());
            throw new PlugError("插件运行io异常");
        }
    }

    /**
     * 歌词
     *
     * @param lyricParam 歌词参数
     * @return 歌词信息
     * @throws IOException
     */
    public LyircResult Lyirc(LyricParam lyricParam) throws PlugError {
        Connection connect = Jsoup.connect(JsososoConstant.LYRIC);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);
        connect.userAgent(JsososoConstant.USERAGENT);
        connect.headers(JsososoConstant.HEADERS);
        if (StringUtils.isEmpty(lyricParam.getType_p()) || StringUtils.isEmpty(lyricParam.getId())) {
            throw new PlugError("请填写相关参数");
        }
        connect.data("id", lyricParam.getId());
        connect.data("_p", lyricParam.getType_p());
        connect.data("_t", LocalDateTime.now().getSecond() + "");
        String text1 = null;
        try {
            text1 = connect.get().text();
        } catch (IOException e) {
            log.error("插件运行异常："+e.getMessage());
            throw new PlugError("插件运行io异常");
        }
        log.debug(text1);
        String text = null;
        try {
            text = connect.get().body().text();
        } catch (IOException e) {
            log.error("插件运行异常："+e.getMessage());
            throw new PlugError("插件运行io异常");
        }
        return new Gson().fromJson(text, LyircResult.class);
    }


}
